package com.java.mall_imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.mall_imooc.common.utils.Constant;
import com.java.mall_imooc.common.utils.PageUtils;
import com.java.mall_imooc.entity.Category;
import com.java.mall_imooc.mapper.CategoryDao;
import com.java.mall_imooc.service.CategoryService;
import com.java.mall_imooc.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Override
    public PageUtils queryPage(Category category,Integer pageNum, Integer pageSize) {
        IPage<Category> page = this.page(
                new Page<>(pageNum, pageSize),
                new QueryWrapper<Category>() // 拼接查询条件
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryVo> categoryTree() {
        List<Category> categories = this.baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getStatus, Constant.ZERO));
        return categories.stream().filter(e -> e.getParentId().equals(Constant.ZERO)).sorted(Comparator.comparing(Category::getSortOrder).reversed()).map(e -> covert(e, categories)).collect(Collectors.toList());
    }

    /**
     * 递归封装children属性
     * 当filter条件不满足时跳出递归
     */
    private CategoryVo covert(Category category, List<Category> categories) {
        CategoryVo node = new CategoryVo();
        BeanUtils.copyProperties(category, node);
        // 排序,默认升序
        List<CategoryVo> children = categories.stream().filter(e -> e.getParentId().equals(category.getId())).map(e -> covert(e, categories)).sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed()).collect(Collectors.toList());
        node.setSubCategories(children);
        return node;
    }

    @Override
    public Set<Integer> subCategoryIds(Integer parentId) {
        List<Category> categories = this.baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getStatus, Constant.ZERO));
        Set<Integer> set = new HashSet<>();
        this.subCovert(parentId,categories,set);
        return set;
    }

    private void subCovert(Integer parentId,List<Category> categories,Set<Integer> set) {
//        for (Category category : categories) {
//            if (category.getParentId().equals(parentId)) {
//                set.add(category.getId());
//                subCovert(category.getId(),categories,set);
//            }
//        }
        categories.stream().filter(e->e.getParentId().equals(parentId)).forEach(e->{
            set.add(e.getId());
            subCovert(e.getId(),categories,set);
        });
    }



}